﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Codeplex.Unity.AutoRegister.Config;
using Microsoft.Practices.Unity;

namespace Codeplex.Unity.AutoRegister.Test.Config
{
    /// <summary>
    /// RegistTypeTraitTest のテスト
    /// </summary>
    [TestClass]
    public class RegistTypeTraitTest
    {
        [TestMethod]
        public void ConstructTest()
        {
            var trait = new RegistTypeTrait(typeof(Drived));
            trait.Is(
                o => o.Name == null && o.LifetimeManager == typeof(PerResolveLifetimeManager) &&
                    o.ToType == typeof(Drived) && o.FromType == typeof(Drived) &&
                    o.InjectionMembers.Length == 0);
        }

        [TestMethod]
        public void FromTypeTest()
        {
            var trait = new RegistTypeTrait(typeof(Drived)) { FromType = typeof(Base) };
            trait.Is(o => o.ToType == typeof(Drived) && o.FromType == typeof(Base));
        }

        [TestMethod]
        public void LifetimeTest()
        {
            var trait = new RegistTypeTrait(typeof(Drived));
            AssertEx.Throws<ArgumentException>(() => trait.LifetimeManager = typeof(Base));
            AssertEx.DoesNotThrow(() => trait.LifetimeManager = typeof(SynchronizedLifetimeManager));
        }

        [TestMethod]
        public void InjectionMemberProviderTest()
        {
            var trait = new RegistTypeTrait(typeof(Drived));
            AssertEx.Throws<ArgumentException>(() => trait.InjectionMemberProvider = typeof(Base));
            AssertEx.DoesNotThrow(() => trait.InjectionMemberProvider = typeof(DummyInjectionMemberProvider));
        }

        [TestMethod]
        public void InvalidFromTypeTest()
        {
            AssertEx.Throws<ArgumentException>(() =>
                new RegistTypeTrait(typeof(Drived)) { FromType = typeof(Other) });
        }

        class Other { }
        class Base { }
        class Drived : Base { }
        class DummyInjectionMemberProvider : InjectionMemberProvider
        {
            public override IEnumerable<InjectionMember> GetInjectionMembers()
            {
                throw new NotImplementedException();
            }
        }


    }

}
